package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.ExceptionHandler.GuliException;
import com.atguigu.vod.Utils.InitVodClient;
import com.atguigu.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;
    //上传视频到阿里云
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(MultipartFile file){
        String videoId = vodService.uploadVideoAliyun(file);
        return R.ok().data("videoId",videoId);
    }
    //根据视频id删除阿里云视频
    @DeleteMapping("deleteAliyunVideo/{id}")
    public R deleteAliyunVideo(@PathVariable String id){
        try {
            DefaultAcsClient client = InitVodClient.initVodClient("LTAI4G15E8H8nQD7H9uTcE8h", "PDIm9xj0PjKBVLkpzqqyz9paAakjRC");
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    //删除多个阿里云视频的方法
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoList") List<String> videoList){
        vodService.removeBatchVideo(videoList);
        return R.ok();
    }
    @GetMapping("getPlayerAuth/{id}")
    public R getPlayerAuth(@PathVariable String id){
        DefaultAcsClient client = InitVodClient.initVodClient("LTAI4G15E8H8nQD7H9uTcE8h", "PDIm9xj0PjKBVLkpzqqyz9paAakjRC");
        String redioAuth;
        try {
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            response = client.getAcsResponse(request);
            //播放凭证
            System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
            redioAuth =(String) response.getPlayAuth();
            //VideoMeta信息
            return R.ok().data("redioAuth",redioAuth);

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取错误");

        }

    }
}
